/*
 */

package labeledbox;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.font.LineMetrics;

/**
 */
public class TextShape implements Shape {

  // Use this object to DELEGATE most things to. (Most properties are just contained in it)
  AbstractShape abShape = new AbstractShape();
  // Properties in addition to those contained in the AbstractShape above
  String label;  // The textShape's string
  Font font;     // The textShape's font.

  final static Font DEFAULT_FONT = new Font("sans-serif", Font.PLAIN, 12);

  // Constructor
  public TextShape() {

    this.font = DEFAULT_FONT;
  }
  
  
  //////////////////////////////////////////////
  ////// METHODS REQUIRED BY THE INTERFACE /////
  
  public void draw(Graphics g) {
    Graphics myG = g.create();

    // Setup the graphics context with the latest properties
    myG.setFont(font);
    FontMetrics f = myG.getFontMetrics(); // needed to adjust the "top" property to a baseline for string draw
    myG.setColor(abShape.getLineColor());

    if( abShape.getFilled() ) {
      Color colorHandle = myG.getColor();
      myG.setColor(abShape.getFillColor());

      myG.fillRect(abShape.getLeft(), abShape.getTop(), this.getWidth(), this.getHeight());
      myG.setColor(colorHandle);
    }

    // Draw the string so that the top of the string is at the 'top' property (adjust downward to do this)
    myG.drawString(label, abShape.getLeft(), abShape.getTop() + f.getHeight() - f.getDescent());

    myG.dispose();
  } /// end draw() /////



  //  Returns TRUE if the shape contains the point and FALSE otherwise.
  //  The shape's BOUNDING BOX determines whether or not the shape contains the point.
  public boolean contains(int x, int y) {
    if(abShape.contains(x, y)) return true;
    else                       return false;
  }

  public int getLeft() {
    return abShape.getLeft();
  }

  public int getTop() {
    return abShape.getTop();
  }

  public int getWidth() {
    //return abShape.getWidth();
    FontMetrics f = Toolkit.getDefaultToolkit().getFontMetrics(font);
    return f.stringWidth(label);
  }

  public int getHeight() {
    //return abShape.getHeight();
    FontMetrics f = Toolkit.getDefaultToolkit().getFontMetrics(font);
    return f.getHeight();
  }
  ///// end methods required by the interface ////
  ////////////////////////////////////////////////

  ////////////////////////////////////////////////////////////////////////////
  //// Additional methods of AbstractShape that this obj wishes to offer /////

  public Color getFillColor() {  // The background fill color of the TextShape
    return abShape.getFillColor();
  }

  public void setFillColor(Color fillColor) {  // The background fill color of the TextShape
    abShape.setFillColor(fillColor);
  }

  // returns the whether or not the shape's filled property is true
  public boolean getFilled() {    // the bg
    return abShape.getFilled();
  }

  public void setFilled(boolean filled) {  // the bg
    abShape.setFilled(filled);
  }

  // (getLeft defined above)
  public void setLeft(int left) {
    abShape.setLeft(left);
  }

  public Color getLineColor() {  // The color of the text
    return abShape.getLineColor();
  }

  public void setLineColor(Color lineColor) {  // The color of the text
    abShape.setLineColor(lineColor);
  }
  // (getTop defined above)
  public void setTop(int top) {
    abShape.setTop(top);
  }

  ///////////////////////////////////////////////////////////////////
  //// Extra getters & setters for properties unique to this obj ////
  
  public Font getFont() {
    return font;
  }

  public void setFont(Font font) {
    this.font = font;

  }

  public String getLabel() {
    return label;
  }

  public void setLabel(String label) {
    this.label = label;
  }


} //////////////////// end class TextShape /////////////////////////////
